//
//  TapActiononeViewController.m
//  礼物
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "TapActiononeViewController.h"

@interface TapActiononeViewController ()

@end

@implementation TapActiononeViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //视图将要出现的时候刷新tableview
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentPage=20;
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
    self.navigationItem.title=@"好店推荐";
    
    self.tableView = [[UITableView alloc] initWithFrame:MAINSCREEN style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    //取消cell线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self GetAFNetWorkingData];
}


-(void)GetAFNetWorkingData
{
    [self addHeaderRefresh];
    [self addFooterRefresh];
}
//设置下拉刷新
-(void)addHeaderRefresh
{
    //当下拉时候会触发该方法
    self.tableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [AppToolss getAFNetWorkingData:@"http://api.liwushuo.com/v2/brands/editor?limit=20&offset=0" andBlock:^(id data) {
            self.goodStoreModelArray=[NSMutableArray array];
            NSMutableDictionary *dictionary = data;
            NSMutableArray *array = [[dictionary objectForKey:@"data"] objectForKey:@"brands"];
            
            for (NSMutableDictionary *dic in array)
            {
                GoodStoreModel *goodStroeModel = [[GoodStoreModel alloc] init];
                [goodStroeModel setValuesForKeysWithDictionary:dic];
                
                [self.goodStoreModelArray addObject:goodStroeModel];
            }
            
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
        } andError:^(id failure) {
            [self.tableView.header endRefreshing];
        }];
    }];
    [self.tableView.header beginRefreshing];
}

-(void)addFooterRefresh
{
    //当上拉时候会触发该方法
    self.tableView.footer=[MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.currentPage+=20;
        [AppToolss getAFNetWorkingData:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/brands/editor?limit=20&offset=0",self.currentPage]
                              andBlock:^(id data){
                                  self.goodStoreModelArray=[NSMutableArray array];
                                  NSMutableDictionary *dictionary = data;
                                  NSMutableArray *array = [[dictionary objectForKey:@"data"] objectForKey:@"brands"];
                                  
                                  for (NSMutableDictionary *dic in array)
                                  {
                                      GoodStoreModel *goodStroeModel = [[GoodStoreModel alloc] init];
                                      [goodStroeModel setValuesForKeysWithDictionary:dic];
                                      
                                      [self.goodStoreModelArray addObject:goodStroeModel];
                                  }
                                  
                                  [self.tableView reloadData];
                                  [self.tableView.footer endRefreshing];
                              } andError:^(id failure) {
                                  [self.tableView.footer endRefreshing];
                              }];
        
    }];
    
    
}



//指定分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//指定多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.goodStoreModelArray.count;
}

//创建一个cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //创建自定义cell
    
    static NSString *cellIndentifier = @"cell";
    GoodStoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil)
    {
        cell = [[GoodStoreTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//设置cell点击效果 (没有置灰效果)
    
    GoodStoreModel *goodStoreModel = [self.goodStoreModelArray objectAtIndex:indexPath.row];
    cell.model = goodStoreModel;
    return cell;
}

//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return (HEIGHT - 113) / 2;
    
}

//点击cell触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TapActionOneDetailViewController *goodStoreDetailVC = [[TapActionOneDetailViewController alloc] init];
    GoodStoreModel *goodStoreModel = [self.goodStoreModelArray objectAtIndex:indexPath.row];
    goodStoreDetailVC.goodStoreModel = goodStoreModel;//属性传值
    
    //push到详情的时候让tabBar(工具栏)消失
    goodStoreDetailVC.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:goodStoreDetailVC animated:YES];
    
}

-(void)leftBarButtonItemAction:(UIBarButtonItem *)barButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
